// 引入express模块
const express = require("express")
// 定义路由级中间件
const router = express.Router()
// 引入数据模型模块
const Article = require("../model/article")

// 查询所有文章信息路由
router.get("/article", (req, res) => {
  const searchRegex = req.query.inputContent
  if (searchRegex === '') {
    Article.find({})
      .sort({ update_at: -1 })
      .then(articles => {
        res.json(articles)
      })
      .catch(err => {
        console.log('查询报错')
        console.log(err)
        res.json(err)
      })
  } else {
    Article.find(
      {
        $or: [
          {
            title: { $regex: eval('/' + searchRegex + '/') }
          },
          {
            category: { $regex: eval('/' + searchRegex + '/') }
          },
          {
            content: { $regex: eval('/' + searchRegex + '/') }
          },
          {
            intro_content: { $regex: eval('/' + searchRegex + '/') }
          }
        ]
      }
    )
      .sort({ update_at: -1 })
      .then(articles => {
        res.json(articles)
      })
      .catch(err => {
        console.log('查询报错')
        console.log(err)
        res.json(err)
      })
  }
})

// 根据分类查询对应文章
router.get("/article_cates", (req, res) => {
  Article.find({ category: req.query.cate })
    .sort({ update_at: -1 })
    .then(articles => {
      res.json(articles)
    })
    .catch(err => {
      console.log('查询报错')
      console.log(err)
      res.json(err)
    })
})

// 通过ObjectId获取单篇文章信息路由
router.get("/article/:id", (req, res) => {
  Article.findById(req.params.id)
    .then(articles => {
      res.json(articles);
    })
    .catch(err => {
      console.log(err)
      res.json(err);
    });
});

// 修改文章信息
router.put("/articleChange/:id", (req, res) => {
  Article.findOneAndUpdate(
    { _id: req.params.id },
    {
      $set: {
        title: req.body.title,
        category: req.body.category,
        content: req.body.content,
        intro_content: req.body.content
      }
    },
    {}
  )
    .then(
      articles => res.json(articles),
    )
    .catch(err =>
      res.json(err),
    )
})

// 更新文章的阅读量
router.put("/article/:id", (req, res) => {
  Article.findOneAndUpdate(
    { _id: req.params.id },
    {
      $inc: {
        visits: 1
      }
    },
    {
      new: true
    }
  )
    .then(articles => res.json(articles))
    .catch(err => res.json(err));
});

// 更新点赞数
router.put("/dianzan/:id", (req, res) => {
  if (req.body.state === true) {
    Article.findOneAndUpdate(
      { _id: req.params.id },
      {
        $inc: {
          dianzan: 1
        }
      },
      {
        new: true
      }
    )
      .then(articles => res.json(articles))
      .catch(err => res.json(err));
  } else if (req.body.state === false) {
    Article.findOneAndUpdate(
      { _id: req.params.id },
      {
        $inc: {
          dianzan: -1
        }
      },
      {
        new: true
      }
    )
      .then(articles => res.json(articles))
      .catch(err => res.json(err));
  } else {
    console.log("输入的参数有误！")
      .then(articles => res.json(articles))
      .catch(err => res.json(err));
  }
})

// 发布一篇文章
router.post("/articleRelease", (req, res) => {
  Article.create(req.body, (err, article) => {
    if (err) {
      res.json(err)
    } else {
      res.json(article)
    }
  })
})

// 删除一篇文章
router.delete("/articleDelete/:id", (req, res) => {
  Article.findByIdAndRemove({
    _id: req.params.id
  })
    .then(articles => res.json(articles))
    .catch(err => res.json(err))
})

module.exports = router
